<?php
	include ('../config/validacao.php');

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"http://www.w3.org/TR/html4/strict.dtd">
<html lang="pt_BR">
<head>
	<meta charset="utf-8">
	<link rel="icon" type="image/png" href="../images/icone.png">
	<title><?php include('../titulo.php')?></title>
	<script type="text/javascript" src="../js/jquery-1.7.2.min.js"></script>
 	
	<link rel="stylesheet" href="../css/jquery-ui-1.8.21.custom.css" type="text/css" media="all" />
	<link rel="stylesheet" href="../css/index.css"  media="screen"> 
	<link rel="stylesheet" href="../bootstrap/css/bootstrap.min.css"  media="screen"> 
	
	<script type="text/javascript" src="../bootstrap/js/bootstrap.min.js"></script> 
	<script type="text/javascript" src="../js/bootstrap-inputmask.js"/></script>
	
    <script type="text/javascript" src="../js/jquery.validate.min.js"></script> 
	<script type="text/javascript" language="javascript" >
		$(document).ready(function(){
			$('#form').validate({  
				rules: {  
					
					setor: { required: true },
					ramal: { required: true }
					
				},  
				messages: {  
					setor: { required: 'Preencha o campo'},
					ramal: { required: 'Preencha o campo'}
				},
				submitHandler: function( form ){  
					var dados = $( form ).serialize();  
					$.ajax({  
						type: "POST",  
						url: "c_contato.php",  
						data: dados,  
						success: function( data )  
						{  
							alert( data );
							window.location.href = 'cad_contato.php';
							$("input[name='contato']").val('');
							$("input[name='cargo']").val('');
							$("input[name='email']").val('');
							$("input[name='contato']").focus();
						}  
					});  
					return false;  
				} 
			}); 
			$("input#contato").each(function(){
				$(this).rules("add", {
					required:true,
					remote : {
						url : 'valida_contato.php',
						type : 'get',
						data : {
							'contato':function(){
								return $('input#contato').val();},
							'ramal': function(){
								return $('#ramal').val();}
						}
						
					},
					messages:{
						required: 'Preencha o campo',
						remote:'Contato cadastrado'
					}
				});                   
			});
			$("input#email").each(function(){
                        	$(this).rules("add", {email: true});
                        });

		}); 
	</script>
	<script type="text/javascript" charset="utf-8">
	$(function(){
	  $("select#secretaria").change(function(){
		$.getJSON("../c_setor/seleciona_setor.php",{id: $(this).val(), ajax: 'true'}, function(j){
		  var options = '';
                  options += '<option value="0">Selecione um setor</option>';
		  for (var i = 0; i < j.length; i++) {
			options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
		  }
		  $("select#setor").html(options);
		})
	  })
	})
	 $(function(){
          $("select#setor").change(function(){
                $.getJSON("../c_ramal/seleciona_ramal.php",{id: $(this).val(), ajax: 'true'}, function(j){
                  var options = '';
                  //options += '<option value="0"></option>';
                  for (var i = 0; i < j.length; i++) {
                        options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
                  }
                  $("select#ramal").html(options);
                })
          })
        })

	$(function () {
		function removeCampo() {
			$(".removerCampo").unbind("click");
			$(".removerCampo").bind("click", function () {
				i=0;
				$(".contatos p.camposcontatos").each(function () {
					i++;
				});
				if (i>1) {
					$(this).parent().remove();
				}
			});
		}
		removeCampo();
		$(".adicionarCampo").click(function () {
			novoCampo = $(".contatos p.camposcontatos:first").clone();
			novoCampo.find("input").val("");
			novoCampo.insertAfter(".contatos p.camposcontatos:last");
			removeCampo();
		});
	});
	</script>	
</head>
<body onload="$('#consulta').select()">
	<div id="container">
		<div id="topo" style="text-align:left;position:absolute;">
			<?php include('../cabecalho.php') ?>
		</div>
		<br><br>
		<div id="body">
			<form id="form" class="form-horizontal" role="form">
		<!--	<form id="form" action="c_contato.php" method="post"> -->
				<fieldset>
				<legend>Cadastro de Contato</legend>
					<div class="form-group">
					<label for="" class="col-sm-2 control-label">Local</label>
					<div class="col-sm-2">
					<?php 
					header ('Content-type: text/html; charset=UTF-8');
								include ('../config/conexao.php');

								// query the database table for zip codes that match 'term'
								$rs = mysql_query('SELECT idsecretaria, sigla, nomeSecretaria FROM secretaria
																 ORDER BY sigla ASC');

								// loop through each zipcode returned and format the response for jQuery
								$data = array();
								if ( $rs && mysql_num_rows($rs) )
								{
										while( $row = mysql_fetch_array($rs) )
										{
												echo '<option value='.$row["idsecretaria"].'>'.utf8_encode($row["sigla"]).' - '.utf8_encode($row["nomeSecretaria"]).'</option>';
										}
								}
					?>
					</div>
					</div>
				<label>Local</label>
				<select id="secretaria" name="secretaria" class="span5">
						<option value="">SELECIONE LOCAL </option>
						<?php
								header ('Content-type: text/html; charset=UTF-8');
								include ('../config/conexao.php');

								// query the database table for zip codes that match 'term'
								$rs = mysql_query('SELECT idsecretaria, sigla, nomeSecretaria FROM secretaria
																 ORDER BY sigla ASC');

								// loop through each zipcode returned and format the response for jQuery
								$data = array();
								if ( $rs && mysql_num_rows($rs) )
								{
										while( $row = mysql_fetch_array($rs) )
										{
												echo '<option value='.$row["idsecretaria"].'>'.utf8_encode($row["sigla"]).' - '.utf8_encode($row["nomeSecretaria"]).'</option>';
										}
								}
						?>
				</select>

				<label>Setor</label>
				<select id="setor" name="setor" class="span4">
					<option value="">AGUARDE...</option>
					<?php
					//	header ('Content-type: text/html; charset=UTF-8'); 
					//	include ('../config/conexao.php');
						 
						// query the database table for zip codes that match 'term'
						$rs = mysql_query('SELECT idsetor, nomeSetor FROM setora ORDER BY nomeSetor ASC');

						// loop through each zipcode returned and format the response for jQuery
						$data = array();
						if ( $rs && mysql_num_rows($rs) )
						{
							while( $row = mysql_fetch_array($rs) )
							{
								echo '<option value='.$row["idsetor"].'>'.utf8_encode($row["nomeSetor"]).'</option>';
							}
						}
					?>
				</select>
				<label>Ramais do Setor</label>
				<select id="ramal" name="ramal" class="span2">
					<option value="">AGUARDE...</option>
				</select>
				<br>
				</fieldset>
				<fieldset>
				<legend>Contatos</legend>
				<div class="contatos">
					<p class="camposcontatos">
						Nome:<input class="input-xlarge" id="contato" type="text" name="contato[]" onChange="javascript:this.value=this.value.toUpperCase();"/>
						Cargo:<input class="input-xlarge" type="text" name="cargo[]" onChange="javascript:this.value=this.value.toUpperCase();"/>
						E-mail:<input class="input-xlarge" id="email" type="text" name="email[]"/>
						Tag:<input class="input-xlarge" type="text" name="tag[]" onChange="javascript:this.value=this.value.toUpperCase();"/>
						<a href="#" class="removerCampo"><img src="../images/remove.png"/></a>
					</p>
				</div>
				<p>
					<a href="#" class="adicionarCampo"><img src="../images/add.png"/></a>
				</p>
				
				<br>
				<button type="submit" class="btn">Cadastrar</button>
				</fieldset>
				</form>
		</div>
	</div>
</body>
</html>
